package www.ld.com.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ld.com.dictionary.UseStatusEnum;
import www.ld.com.domain.department.pojo.DepartmentPO;
import www.ld.com.domain.department.query.DepartmentQuery;
import www.ld.com.domain.vo.SelectTreeVO;
import www.ld.com.exception.ValidateException;
import www.ld.com.mapper.DepartmentMapper;
import www.ld.com.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UserMapper userMapper;
    public List<DepartmentPO> findDepartmentPO(DepartmentQuery departmentQuery){
         return   departmentMapper.selectList(
                new LambdaQueryWrapper<DepartmentPO>()
                        .select(DepartmentPO::getId
                                ,DepartmentPO::getSort
                                ,DepartmentPO::getName
                                , DepartmentPO::getStatus
                                , DepartmentPO::getParentId
                        )
                        .like(StrUtil.isNotBlank(departmentQuery.getName()), DepartmentPO::getName, departmentQuery.getName())
                        .eq(departmentQuery.getStatus() != null, DepartmentPO::getStatus, departmentQuery.getStatus())
                        .orderByAsc(DepartmentPO::getSort)
        );
    }
    @Transactional
    public void saveDepartmentPO(DepartmentPO departmentPO){
        departmentMapper.insert(departmentPO);
    }
    @Transactional
    public void updateDepartmentPOById(DepartmentPO departmentPO){
        if (departmentPO.getStatus() == UseStatusEnum.STOP_USING && userMapper.existDepartment(String.valueOf(departmentPO.getId()))) {
            throw new ValidateException("修改失败：该部门有用户使用,岗位状态不能改为“停用”");
        }
        departmentMapper.updateById(departmentPO);
    }


    public DepartmentPO getDepartmentPOById(String id){
        return departmentMapper.selectById(id);
    }

    public void  deleteDepartmentById(String id){
        Integer count = departmentMapper.selectCount(new LambdaQueryWrapper<DepartmentPO>().eq(DepartmentPO::getParentId, id));
        if(count.compareTo(0)==1){
            throw new ValidateException("删除部门失败:部门中存在下级部门");
        }
        if(userMapper.existDepartment(id))
            throw new ValidateException("删除部门失败：该部门有用户在使用");
        departmentMapper.deleteById(id);
    }

    public List<SelectTreeVO>  findSelectVOAll(){
        List<DepartmentPO> departmentPOS = departmentMapper.selectList(
                new LambdaQueryWrapper<DepartmentPO>()
                        .select(DepartmentPO::getName,DepartmentPO::getStatus, DepartmentPO::getId,DepartmentPO::getParentId)
                        .orderByAsc(DepartmentPO::getSort).orderByAsc(DepartmentPO::getId)
        );
        return departmentPOS.stream().map(v -> {
            return BeanUtil.toBean(v, SelectTreeVO.class);
        }).collect(Collectors.toList());
    }

}
